const express = require('express');
const router = express.Router();
// 引入刚才定义的表
const response = require('../utils/response')
const { auth, SECRET } = require('../utils/auth')
// 读写文件
const formidable = require('formidable');
/**
 * 这里是不需要token的api
 * @route POST /upload
 * @summary upload上传
 * @group upload - 组注释
 * @param {file} file.formData.required
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.post('/', auth, async (req, res, next) => {
  const form = formidable({
    multiples: true,
    keepExtensions: true,
    uploadDir: "./public/uploads/"
  });

  // form.on('progress', (bytesReceived, bytesExpected) => {
  //   console.log(bytesReceived, bytesExpected);    
  // });

  form.parse(req, (err, fields, files) => {
    if (err) {
      console.log(err)
      next(err);
      return;
    }
    console.log({ fields, files })
    response(res, 0, "成功", files)
  });
});

module.exports = router;
